/* *
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

function ListNode(val) {
  this.val = val;
  this.next = null;
}
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
  if (!head || !head.next) {
    return head;
  }
  let prev = null;
  // 存在三个指针：prev head next，每次都反转一个指针head.next = prev;
  while (head) {
    let next = head.next;
    head.next = prev; // 每次循环只反转一个元素

    prev = head;
    head = next;
  }
  return prev;
};
